//
//  ScrollFloatHandler.swift
//  zhonglibang
//
//  Created by Rowling on 2021/1/5.
//

import UIKit
class ScrollFloatHandler : NSObject {
    // 父滚动时图
    weak var bgScollView : UIScrollView!
    // 当前子滚动时图
    var subScollObjc : SubScrollDelegate! {
        didSet {
            subScollObjc.scrollViewDidScroll {[weak self] (scollView)  in
                guard let self = self else {return}
                self.scrollViewDidScroll(scollView)
            }
        }
    }
    
    var currentScrollView : UIScrollView {
        get {
            return subScollObjc.scrollView()
        }
    }
    
    //浮动view
    var floatView : UIView!
    // 浮动距离
    var floatH : CGFloat {
        get {
            return floatView.zm.y + floatView.zm.height
        }
    }
    // 当前滚动方向
    private var dirction : UISwipeGestureRecognizer.Direction = .down
    private var subDirction : UISwipeGestureRecognizer.Direction = .down

    private var beforeBgScrollViewOffset = CGPoint.zero
    private var beforeSubViewOffset  = CGPoint.zero
    private var isHover = false
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
//        return
        // 处理背景view
//        print("bgScollView=" + bgScollView.isScrollEnabled.description)
//        print("currentScrollView=" + currentScrollView.isScrollEnabled.description)

        if scrollView == bgScollView {
            let y = scrollView.contentOffset.y
            if (self.beforeBgScrollViewOffset.y < y) {
                self.dirction = .up;
            }
            if (self.beforeBgScrollViewOffset.y > y) {
                self.dirction = .down;
            }
            if (y > floatH || currentScrollView.contentOffset.y > 0) {
                bgScollView.contentOffset = CGPoint(x: 0, y: floatH)
            }
            beforeBgScrollViewOffset = scrollView.contentOffset
        }
        // 处理当前滚动的子view
        if currentScrollView == scrollView {
            let y = scrollView.contentOffset.y
            if (self.beforeSubViewOffset.y < y) {
                subDirction = .up;
            }
            if (self.beforeSubViewOffset.y > y) {
                subDirction = .down;
            }
            
            if bgScollView.contentOffset.y < floatH && subDirction == .up {
                currentScrollView.contentOffset = CGPoint.zero
            } else if (currentScrollView.contentOffset.y < 0) {
                currentScrollView.contentOffset = CGPoint.zero
            }
            if currentScrollView.contentOffset.y <= 0 {
            }
            beforeSubViewOffset = scrollView.contentOffset
        }
    }
}

extension ScrollFloatHandler : ScrollGestureDelegate {
    func mainScrollShouldRecognizeSimultaneouslyWithGestureRecognizer(gestureRecognizer: UIGestureRecognizer, otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        if gestureRecognizer.view?.tag == 999 || otherGestureRecognizer.view?.tag == 999 {
            return false
        }
        else
        {
            return true
        }
    }
}
